package com.hclc.yxb.shiro;

import com.hclc.util.password.BCryptPasswordEncoder;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.stereotype.Component;

/**
 * @ClassName CustomizeHashedCredentialsMatcher
 * @Author: yurj
 * @Mail：1638234804@qq.com
 * @Date: Create in 15:30 2020/10/14
 * @version: 1.0
 */
@Component
public class CustomizeHashedCredentialsMatcher extends SimpleCredentialsMatcher {
    /**
     * 自定义密码验证规则
     *
     * @param token
     * @param info
     * @return boolean
     * @author yurj
     * @version 1.0
     * @date 2020/10/14 15:40
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        SimpleAuthenticationInfo simpleAuthenticationInfo = (SimpleAuthenticationInfo) info;
        // 加密策略
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(String.valueOf(usernamePasswordToken.getPassword()), String.valueOf(simpleAuthenticationInfo.getCredentials()));
    }
}
